package metrics

import (
	"context"
	prometheus2 "github.com/prometheus/client_golang/prometheus"
	"jk-time/webook/internal/service/sms"
	"jk-time/webook/internal/service/sms/memory"
	"time"
)

type PrometheusDecorator struct {
	svc     sms.Service
	summary *prometheus2.SummaryVec
}

func NewPrometheusDecorator() sms.Service {
	labels := []string{"biz"}
	summary := prometheus2.NewSummaryVec(prometheus2.SummaryOpts{ //摘要
		Namespace: "sms_code",
		Subsystem: "metrics",
		Name:      "sms_memory",
		Help:      "监听服务短信",
		ConstLabels: map[string]string{
			"db":          "webook",
			"idstance_id": "sms-1",
		},
		Objectives: map[float64]float64{
			0.5:   0.01,
			0.9:   0.05,
			0.99:  0.001,
			0.999: 0.0001,
		},
	}, labels)
	prometheus2.MustRegister(summary)
	return &PrometheusDecorator{
		summary: summary,
		svc:     memory.NewMemoryService(),
	}
}
func (p *PrometheusDecorator) Send(ctx context.Context, biz string, args []string, numbers ...string) error {
	start := time.Now()
	defer func() {
		dura := time.Since(start)
		p.summary.WithLabelValues(biz).Observe(float64(dura.Milliseconds()))
	}()
	return p.svc.Send(ctx, biz, args, numbers...)
}
